<?php
require_once('dl_converter_functions.php');

/*
 * SPECIFICATION
 *
 * RAWDATA DIRECTORY
 *   SENSORID/*.txt
 *
 * DATA DIRECTORY (one file per day)
 *    SENSORID/yyyymmdd.txt
 *
 * CO2 DIRECTORY (one file per moth)
 *    yyyymm.txt
 *
 */


// ---------------------------
// GLOBAL CONFIGURATION
// ---------------------------
$BASEDIR="DIR";                 //relative path to data root directory
$RAWDATA_DIR="$BASEDIR/dl";    //relative path to data directory
$DATA_DIR="$BASEDIR/data";     //relative path to data directory
$CO2_DIR="$BASEDIR/co2";       //relative path to co2 output directory

$PPM_PER_VOLT=200;  //scale factor

$OUTPUT_SAMPLING_RATE = 10 * 60; // seconds
$RAWDATA_FILES_DIR = $DATA_DIR . "/files";

$MIRAMON_SENSOR_ID  = "SEN001";


/**
 *
 * Gets the date and creates the file with the format wanted
 * @param string $line
 * @return string $name 		Name of the file
 */
function get_file_name($line){
	$token = strtok($line," ");
	$date = $token;
	$token = strtok($date,"/");
	$day_before = $token;
	$month_before = strtok("/");
	$year = strtok("/");
	$name = "$year"."$month_before"."$day_before";
	return $name;
}


/**
 *
 * Gets the day from a string that contains information
 * @param string $line 			String with information (date, hour...)
 * @return string $day
 */
function get_day($line){
	$token = strtok($line," ");
	$date = $token;
	$token = strtok($date,"/");
	$day = $token;
	return $day;
}


/**
 *
 * Gets the month from a string that contains information
 * @param string $line			String with information (date, hour...)
 * @return string $month
 */
function get_month($line){
	$token = strtok($line," ");
	$date = $token;
	$token = strtok($date,"/");
	$month = strtok("/");
	return $month;
}


/**
 *
 * Gets the year from a string that contains information
 * @param string $line			String with information (date, hour...)
 * @return string $year
 */
function get_year($line){
	$token = strtok($line," ");
	$date = $token;
	$token = strtok($date,"/");
	$month_before = strtok("/");
	$year = strtok("/");
	return $year;
}


/**
 *
 * Returns the hour of the line
 * @param string $line
 * @return array $values		Writes into the array the hour of the string given
 */
function get_hour($line){
	$token = strtok($line," ");
	$date = $token;
	$time = strtok(" ");
	$token = strtok($time,":");
	$values["hour"] = $token;
	return $values["hour"];
}


/**
 *
 * Returns the minute of the line
 * @param string $line
 * @return array $values		Writes into the array the minute of the string given
 */
function get_minute($line){
	$token = strtok($line," ");
	$date = $token;
	$time = strtok(" ");
	$token = strtok($time,":");
	$values["hour"] = $token;
	$values["minutes"] = strtok(":");
	return $values["minutes"];
}

/**
 *
 * Parses the line and returns the voltage value as float.
 * @param string $line
 * @return float
 */
function getVoltage($line){
  //10/06/2010 0:00:00            0.668

  $date = strtok($line," ");
  $time = strtok(" ");
  $value = strtok(" ");
  $voltage = floatval($value);
  return $voltage;
}


/**
 *
 * Returns the name of the file, before the '.'
 * @param file $file
 * @return string
 */
function get_name($file){
	$elem_name = explode(".",$file);
	$name = $elem_name[0];
	return $name;
}


/**
 *
 * Returns the extension of the file
 * @param file $file
 * @return string
 */
function get_extension($file){
	$elem_name = explode(".",$file);
	$extension  = $elem_name[1];
	return $extension;
}


/**
 *
 * Sorts the files of the dir by name
 * @param dir $dir 		Directory
 * @return dir 			Sorted directory
 */
function sort_dir($directory){
	$dir = opendir($directory);
	$i=0;
	while (($element = readdir($dir))!= false){
		$elements[$i]=$element;
		$i++;
	}
	sort($elements);
	closedir($dir);
	return $elements;
}


/**
 *
 * Sorts the ".txt" files of the dir by name
 * @param dir $dir 		Directory
 * @return dir 			Sorted ".txt" directory
 */
function sort_txt_dir($dir){
	$i=0;
	$elements = array();
	while (($element = readdir($dir))!= false){
		if (get_txt_extension($element) == "txt"){
			$elements[$i]=$element;
			$i++;
		}
	}
	sort($elements);
	return $elements;
}


/**
 *
 * Returns the extension of the file
 * @param file $file
 * @return string
 */
function get_txt_extension($file){
	$elem_name = explode(".",$file);
	$extension  = $elem_name[sizeof($elem_name)-1];
	return $extension;
}


/**
 *
 * Gets the day from a string that contains information
 * @param string $line 			String with information (date, hour...)
 * @return string $day
 */
function get_date($path, $f){
	$file = fopen("$path/$f", "a+");
	$i = 1;
	while ($i <= 3){
		$line = fgets($file);
		$i++;
	}
	fclose($file);
	$time = array();
	$time[0] = get_hour($line);
	$time[1] = get_minute($line);
	return $time;
}


/**
 *
 * Moves processed files to "processed" folder
 * @param string $oldfile
 * @param string $newfile
 */
function move_processed($oldfile, $newfile){
  // to avoid warning, first copy and them remove the original
  copy($oldfile,$newfile);
  unlink($oldfile);

}


/**
 *
 * Deletes same lines and writes into a new file
 * @param $array 			Array of [ts, lines]
 * @param $path
 * @param string $name 		Name of the new file
 */
function delete_same($array, $path, $name){
	$k=0;
	$f = fopen("$path/$name.X.txt", 'w+');
	$header = get_header("$name.txt", $path);
	fwrite($f, $header["first"]);
	fwrite($f, $header["second"]);
	while ($k < sizeof($array)-1){
		if ($array[$k]["ts"] != $array[$k+1]["ts"]){
			fwrite($f, $array[$k]["line"]);
		}
		$k++;
	}
	fwrite($f, $array[$k]["line"]);
	fclose($f);
}


/**
 *
 * Gets the header of the file given
 * @param file $file
 * @param string $path
 * @return array $header 	Returns header. Two lines.
 */
function get_header($file, $path){
	$f = fopen("$path/$file", 'r+');
	$header = array();
	$header["first"] = fgets($f);
	$header["second"] = fgets($f);
	fclose($f);
	return $header;
}


/**
 *
 * Sorts the array given by the "ts" sub-array
 * @param array $array
 * @param ('ts', 'line') $sort_by
 * @return array $array 		Returns sorted array
 */
function sort_array($array, $sort_by) {
	foreach ($array as $key => $value) {
		$evalstring = '';
		foreach ($sort_by as $sort_field) {
			$tmp[$sort_field][$key] = $value[$sort_field];
			$evalstring .= '$tmp[\'' . $sort_field . '\'], ';
		}
	}
	$evalstring .= '$array';
	$evalstring = 'array_multisort(' . $evalstring . ');';
	eval($evalstring);
	return $array;
}


/**
 *
 * Gets an array from same named files with 'ts' and 'line' info.
 * @param file $elem1
 * @param file $elem2
 * @param string $path
 * @return array $array 		Array with [ts1, line1, ts2, line2, ..., tsn, linen] format.
 */
function get_array($elem1, $elem2, $path){
	$file = $elem1;
	$file_name = explode(".",$file);
	$name = $file_name[0];
	$f2 = fopen("$path/$elem1", 'r');
	$f1 = fopen("$path/$elem2", 'r');
	$array = array();
	for ($i=0; $i<2; $i++){
		$line1 = fgets($f1);
	}
	$j=0;
	while (!feof($f1)){
		$line1 = fgets($f1);
		//lehenengo ts
		$ts1 = get_timestamp($line1);
		$values["ts"] = $ts1;
		$values["line"] = $line1;
		$array[$j] = $values;
		$j++;
	}
	fclose($f1);
	for ($i=0; $i<2; $i++){
		$line2 = fgets($f2);
	}
	while (!feof($f2)){
		$line2 = fgets($f2);
		//lehenengo ts
		$ts2 = get_timestamp($line2);
		$values["ts"] = $ts2;
		$values["line"] = $line2;
		$array[$j] = $values;
		$j++;
	}
	fclose($f2);
	return $array;
}


/**
 *
 * Gets timestamp from date given
 * @param string $line
 * @return int $ts 		Returns timestamp
 */
function get_timestamp($line){
	$token = strtok($line," ");
	$date = $token;
	$time = strtok(" ");
	$token = strtok($date,"/");
	$values["day"] = $token;
	$values["month"] = strtok("/");
	$values["year"] = strtok("/");
	$token = strtok($time,":");
	$values["hour"] = $token;
	$values["minutes"] = strtok(":");
	$values["seconds"] = strtok(":");
	$ts = mktime((int)$values["hour"], (int)$values["minutes"], (int)$values["seconds"], (int)$values["month"], (int)$values["day"], (int)$values["year"]);
	return $ts;
}
?>